﻿using UnityEngine;
using System.Collections;
using System.IO;

namespace AutoRoot
{
   public class Initialization : MonoBehaviour
   {
      public GameObject dropDownMenu;
      public GameObject dropDownMenuKnob;
      public GameObject dropDownMenuList;

      public GameObject progressBar;

      public GameObject[] buttons;

      private static TouchedObjHandler touchedObjHandler = new TouchedObjHandler();
      private static TouchedPositionHandler touchedPositionHandler = new TouchedPositionHandler();

      public TouchedObjHandler TouchedObjHandler
      {
         get { return touchedObjHandler; }
      }
      public TouchedPositionHandler TouchedPositionHandler
      {
         get { return touchedPositionHandler; }
      }

      void Start()
      {

         Downloader.download("http://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg", "ACDC_-_Back_In_Black-sample.ogg");
         Downloader.download("http://upload.wikimedia.org/wikipedia/en/0/04/Rayman_2_music_sample.ogg", "Rayman_2_music_sample.ogg");
         Downloader.download("http://upload.wikimedia.org/wikipedia/en/b/b5/Radiohead_-_Pyramid_Song_%28sample%29.ogg", "Radiohead_-_Pyramid_Song_sample.ogg");
         Downloader.download("http://upload.wikimedia.org/wikipedia/en/9/9f/Sample_of_%22Another_Day_in_Paradise%22.ogg", "Sample_of_Another_Day_in_Paradise.ogg");

         string neusoftXML = @"<?xml version='1.0' encoding='utf-8'?>
<story name='myStory'>
	<section id='a' name='section a' resource='/ACDC_-_Back_In_Black-sample.ogg' decisionResource='/Rayman_2_music_sample.ogg'>
		<section id='a' name='section aa' resource='/Radiohead_-_Pyramid_Song_sample.ogg' decisionResource='/Sample_of_Another_Day_in_Paradise.ogg'>
		</section>
	</section>
</story>";

         File.WriteAllText(Application.persistentDataPath + "/neusoft.xml", neusoftXML);

         touchedObjHandler = new TouchedObjHandler();
         touchedPositionHandler = new TouchedPositionHandler();
         touchedPositionHandler.addListener(dropDownMenu);
         touchedPositionHandler.addListener(progressBar);

         foreach (GameObject button in buttons)
         {
            touchedPositionHandler.addListener(button);
         }

         Screen.orientation = ScreenOrientation.Portrait;
         Screen.autorotateToLandscapeLeft = false;
         Screen.autorotateToLandscapeRight = false;
         Screen.autorotateToPortrait = false;
         Screen.autorotateToPortraitUpsideDown = false;

         Global.currentAudio = Camera.main.audio;

         Global.myStory.ReadFromXML(Application.persistentDataPath + "/neusoft.xml");

         Global.currentSection = Global.myStory.GetLast();

         Global.LoadAudio(Global.currentSection.Value.resourcePath);

         GameObject.Find("ControlPart").SendMessage("OnPlaySection", SendMessageOptions.DontRequireReceiver);
         GameObject.Find("Progressbar").SendMessage("updateTotalTime", Global.currentAudio.clip.length, SendMessageOptions.DontRequireReceiver);

      }
   }
}